﻿<template>
    <div class="SHZY-common-layout">
        <div class="SHZY-common-layout-center">
            <el-row class="SHZY-common-search-box" :gutter="16">
                <el-form @submit.native.prevent="">
                    <el-col :span="6">
                        <el-form-item label="编码">
                            <el-input v-model="query.enCode" placeholder="编码" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="描述">
                            <el-input v-model="query.fullName" placeholder="描述" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
                            <el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
            <div class="SHZY-common-layout-main SHZY-flex-main">
                <div class="SHZY-common-head">
                    <div>
                        <el-button type="primary" icon="el-icon-plus" @click="addOrUpdateHandle()">新增</el-button>
                    </div>
                    <div class="SHZY-common-head-right">
                        <el-tooltip effect="dark" content="刷新" placement="top">
                            <el-link icon="icon-ym icon-ym-Refresh SHZY-common-head-icon" :underline="false"
                                @click="reset()" />
                        </el-tooltip>
                    </div>
                </div>
                <SHZY-table v-loading="listLoading" :data="list">
                    <el-table-column label="编码" prop="enCode" align="left">
                    </el-table-column>
                    <el-table-column label="描述" prop="fullName" align="left">
                    </el-table-column>
                    <el-table-column label="执行接口" prop="execInterfaceId" align="left">
                    </el-table-column>
                    <el-table-column label="异步处理" prop="asyncFlag" align="left">
                        <template slot-scope="scope">
                            <el-tag :type="scope.row.asyncFlag == 'Y' ? '' : 'info'" disable-transitions>
                                {{ scope.row.asyncFlag == 'Y' ? '是' : '否' }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="异常重试次数" prop="retryCount" align="left">
                    </el-table-column>
                    <el-table-column label="定时接口" prop="cronJobFlag" align="left">
                        <template slot-scope="scope">
                            <el-tag :type="scope.row.cronJobFlag == 'Y' ? '' : 'info'" disable-transitions>
                                {{ scope.row.cronJobFlag == 'Y' ? '是' : '否' }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="发送接口" prop="sendInterfaceId" align="left">
                    </el-table-column>
                    <el-table-column label="Cron表达式" prop="cron" align="left">
                    </el-table-column>
                    <el-table-column label="执行方式" prop="executeMethod" align="left">
                        <template slot-scope="scope">
                            <template
                                v-if="(scope.row.cronJobFlag == 'Y' || scope.row.asyncFlag == 'Y') && scope.row.executeMethod == 'Parallel'">并行</template>
                            <template
                                v-if="(scope.row.cronJobFlag == 'Y' || scope.row.asyncFlag == 'Y') && scope.row.executeMethod == 'Serial'">串行</template>
                        </template>
                    </el-table-column>
                    <el-table-column prop="runFlag" label="任务状态" width="70" align="center">
                        <template slot-scope="scope">
                            <el-tooltip effect="dark" :content="scope.row.runFlag == 'Y'?'点击关闭定时任务':'点击启动定时任务'" placement="top">
                                <el-switch v-if="scope.row.cronJobFlag == 'Y'" v-model="scope.row.runFlag" active-value="Y"
                                    inactive-value="N" @click.native="handleUpdateState(scope.row)" disabled
                                    class="table-switch" />
                            </el-tooltip>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column label="缓存" prop="cacheFlag" align="left">
                        <template slot-scope="scope">
                            <el-tag :type="scope.row.cacheFlag == 'Y' ? '' : 'info'" disable-transitions>
                                {{ scope.row.cacheFlag == 'Y' ? '是' : '否' }}</el-tag>
                        </template>
                    </el-table-column> -->
                    <el-table-column label="备注" prop="remark" align="left">
                    </el-table-column>
                    <el-table-column prop="enabledMark" label="状态" width="70" align="center">
                        <template slot-scope="scope">
                            <el-tag :type="scope.row.enabledMark == 1 ? 'success' : 'danger'" disable-transitions>
                                {{ scope.row.enabledMark == 1 ? '正常' : '停用' }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" fixed="right" width="100">
                        <template slot-scope="scope">
                            <el-button type="text" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
                            <el-button type="text" @click="handleDel(scope.row.id)" class='SHZY-table-delBtn'>删除</el-button>
                        </template>
                    </el-table-column>
                </SHZY-table>
                <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize"
                    @pagination="initData" />
            </div>
        </div>
        <Form v-show="formVisible" ref="Form" @close="refresh" />
    </div>
</template>
<script>
import { getInterfaceConfigList, delInterfaceConfig, TimeTaskStop, TimeTaskEnable } from '@/api/system/interface/config'
import Form from './Form'
export default {
    components: { Form },
    name: 'system-interfaceConfig',
    data() {
        return {
            query: {
                enCode: undefined,
                fullName: undefined,
            },
            list: [],
            listLoading: true,
            total: 0,
            listQuery: {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            },
            formVisible: false,
            columnList: [
                { prop: 'enCode', label: '编码' },
                { prop: 'fullName', label: '描述' },
                { prop: 'execInterfaceId', label: '执行接口ID' },
                { prop: 'asyncFlag', label: '异步处理' },
                { prop: 'retryCount', label: '异常重试次数' },
                { prop: 'cronJobFlag', label: '定时接口' },
                { prop: 'sendInterfaceId', label: '发送接口ID' },
                { prop: 'cron', label: 'Cron表达式' },
                { prop: 'executeMethod', label: '执行方式' },
                { prop: 'cacheFlag', label: '缓存' },
                { prop: 'remark', label: '备注' },
                { prop: 'enabledMark', label: '有效标志' },
            ],
        }
    },
    computed: {
        menuId() {
            return this.$route.meta.modelId || ''
        }
    },
    created() {
        this.initData()
    },
    methods: {
        initData() {
            this.listLoading = true;
            let _query = {
                ...this.listQuery,
                ...this.query,
                menuId: this.menuId,
            };
            let query = {}
            for (let key in _query) {
                if (Array.isArray(_query[key])) {
                    query[key] = _query[key].join()
                } else {
                    query[key] = _query[key]
                }
            }
            getInterfaceConfigList(query).then(res => {
                this.list = res.data.list
                this.total = res.data.pagination.total
                this.listLoading = false
            })
        },
        handleDel(id) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                type: 'warning'
            }).then(() => {
                delInterfaceConfig(id).then(res => {
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        onClose: () => {
                            this.initData()
                        }
                    });
                })
            }).catch(() => {
            });
        },
        addOrUpdateHandle(id) {
            this.formVisible = true
            this.$nextTick(() => {
                this.$refs.Form.init(id)
            })
        },
        search() {
            this.listQuery = {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            }
            this.initData()
        },
        refresh(isrRefresh) {
            this.formVisible = false
            if (isrRefresh) this.reset()
        },
        reset() {
            for (let key in this.query) {
                this.query[key] = undefined
            }
            this.listQuery = {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            }
            this.initData()
        },
        handleUpdateState(row) {
            const txt = row.runFlag == "Y" ? '停止' : '启用'
            this.$confirm(`您确定要${txt}该任务, 是否继续?`, '提示', {
                type: 'warning'
            }).then(() => {
                const reqMethod = row.runFlag == "Y" ? TimeTaskStop : TimeTaskEnable
                reqMethod(row.id).then(res => {
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        duration: 1000,
                        onClose: () => {
                            row.runFlag = row.runFlag == "Y" ? "N" : "Y"
                        }
                    })
                })
            }).catch(() => { })
        },
    }
}
</script>